package com.zgz.park.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zgz.park.common.config.Result;
import com.zgz.park.pojo.Admin;
import com.zgz.park.pojo.common.CommonPageReq;
import com.zgz.park.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author willie
 * @since 2022-12-10
 */
@CrossOrigin
@RestController
@RequestMapping("/park/admin")
public class AdminController {

    @Resource
    private AdminService adminServiceImpl;

    /**
     * 条件分页查询所有管理员数据
     * @return
     */
    @PostMapping("/getAdminByPageVo")
    public Result getAllAdminByPage(@RequestBody CommonPageReq queryData){

        IPage<Admin> allAdminByPage = adminServiceImpl.getAllAdminByPage(queryData);
        List<Admin> records = allAdminByPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        long total = allAdminByPage.getTotal();
        map.put("adminList",records);
        map.put("total",total);
        return Result.succ().data(map);
    }


    @GetMapping("/getAdminById")
    public Result getAdminById(@RequestParam("id") String id){
        Map<String,Object> map = new HashMap<>();
        Admin vo = new Admin();
        if (StringUtils.isNotEmpty(id)) {
            vo = adminServiceImpl.getAdminById(id);
        }
        map.put("vo",vo);
        return Result.succ().data(map);
    }

    @PostMapping("/saveSubmit")
    public Result saveSubmit(@RequestBody Admin admin) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        boolean result = adminServiceImpl.saveSubmit(admin);
        if(result){
            return Result.succ().message("操作成功");

        }else{
            return Result.succ().message("操作失败");
        }

    }

    /**
     * 禁用或解禁
     * @param id
     * @return
     */
    @PostMapping("/invalidOrRecovery")
    public Result invalidOrRecovery(@RequestParam("id") String id){

        boolean result = adminServiceImpl.invalidOrRecovery(id);
        if(result){
            return Result.succ().message("操作成功");

        }else{
            return Result.succ().message("操作失败");
        }

    }

    /**
     * 根据id逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam("id") String id){

        boolean result = adminServiceImpl.deleteById(id);
        if(result){
            return Result.succ().message("操作成功");

        }else{
            return Result.succ().message("操作失败");
        }

    }
}

